package com.zto.design.command.lightdemo;

/**
 * @program: sch-diy-parent
 * @description: 简单遥控器--相当于被封装的调用者
 * @author: sch
 * @create: 2020-11-28 23:47
 **/
public class SimpleRemoteControl {
    /**
     * 开、关、撤销命令
     */
    Command slotOn;
    Command slotOff;
    Command undoCommand;

    public SimpleRemoteControl() {
        NoCommand noCommand = new NoCommand();
        slotOn = noCommand;
        slotOff = noCommand;
        undoCommand = noCommand;
    }

    /**
     * 设置开关命令
     *
     * @param slotOn
     * @param slotOff
     */
    public void setCommand(Command slotOn, Command slotOff) {
        this.slotOn = slotOn;
        this.slotOff = slotOff;
    }


    /**
     * 按下打开按钮
     */
    public void onButtonWasPushed() {
        slotOn.execute();
        this.undoCommand = slotOn;
    }

    /**
     * 按下关闭按钮
     */
    public void offButtonWasPshed() {
        slotOff.execute();
        this.undoCommand = slotOff;
    }

    /**
     * 撤销操作
     */
    public void ondoButtonWasPshed() {
        this.undoCommand.undo();
    }
}
